package UI;

import javax.swing.*;
import java.awt.*;
import java.net.Socket;


public class MainFrame extends JFrame {
    private final Socket socket;
    private String username;
        private ContactPanel contactPanel;
        private ChatPanel chatPanel;

        public MainFrame(String username, Socket socket) {
            this.username = username;
            this.socket = socket;

            setTitle("内网通 - " + username);
            setSize(1000, 600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            initUI();
        }

        private void initUI() {
            // 使用分割面板将界面分为联系人列表和聊天区域
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setDividerLocation(250);
            splitPane.setDividerSize(3);

            // 左侧联系人面板
            contactPanel = new ContactPanel(socket,username);
            splitPane.setLeftComponent(contactPanel);

            // 右侧聊天面板
            chatPanel = new ChatPanel(username,socket);
            splitPane.setRightComponent(chatPanel);

            add(splitPane, BorderLayout.CENTER);

            // 设置菜单栏
            setJMenuBar(createMenuBar());
        }

        private JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();

            JMenu fileMenu = new JMenu("文件");
            JMenuItem exitItem = new JMenuItem("退出");
            exitItem.addActionListener(e -> System.exit(0));
            fileMenu.add(exitItem);

            JMenu helpMenu = new JMenu("帮助");
            JMenuItem aboutItem = new JMenuItem("关于");
            aboutItem.addActionListener(e -> {
                JOptionPane.showMessageDialog(this,
                        "内网通聊天软件 v1.0\n企业内部通信工具",
                        "关于", JOptionPane.INFORMATION_MESSAGE);
            });
            helpMenu.add(aboutItem);

            menuBar.add(fileMenu);
            menuBar.add(helpMenu);

            return menuBar;
        }
    }
